﻿//----------------------------------------------------------------
//CodeArt.
//
//文件描述:
//
//创 建 人: jianyi
//Mail:jianyi0115@163.com,
//Blog:http://www.cnblogs.com/jianyi0115
//创建日期: 2014-6-1
//
//修订记录: 
//
//----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Microsoft.SharePoint;


namespace CodeArt.SharePoint.CamlQuery
{
    class TemplateCamlExpression : CamlExpression
    {
        public TemplateCamlExpression(string template, string field, bool isInternalName, string value)
        {
            this.CamlTemplate = template;
            FieldName = field;
            CompareValue = value;
            IsInternalName = isInternalName;  
        }

        public readonly string FieldName;
        public readonly string CompareValue;
        public readonly bool IsInternalName;

        protected void ApppendXml(XmlNode node, string xml)
        {
            if (node.OwnerDocument == null)
                throw new Exception("node.OwnerDocument can't be null");

            XmlDocumentFragment xfrag = node.OwnerDocument.CreateDocumentFragment();
            xfrag.InnerXml = xml;
            node.AppendChild(xfrag);
        }

        public string CamlTemplate
        {
            get;
            set;
        }

        protected virtual String ToCamlXml(ICamlBuildProvider provider, System.Xml.XmlNode parentNode)
        {
            var internalName = base.GetFieldInternalName(provider, this.FieldName, this.IsInternalName);
            return String.Format(this.CamlTemplate, internalName,this.CompareValue);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="parentNode"></param>
        public override void ToCaml(ICamlBuildProvider provider, System.Xml.XmlNode parentNode)
        {
            var xml = this.ToCamlXml(provider, parentNode);
            this.ApppendXml(parentNode, xml);
        }        
    }
//    <Includes> 
//<FieldRef Name='{0}'/> 
//<Value Type='Lookup'>{1}</Value> 
//</Includes>
    //class IncludesExpression : TemplateCamlExpression
    //{
    //    public IncludesExpression(string field, bool isInternalName, string value):base(field,isInternalName,value)
    //    {
    //    }

    //    public override void ToCaml(ICamlBuildProvider provider, XmlNode parentNode)
    //    {
    //        base.ToCaml(provider, parentNode);
    //    }
    //}
}
